package com.minimalist.common.entity.tc.config;

import java.util.Objects;
import okhttp3.OkHttpClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 百度API配置类
 *
 * @author 11987
 */
@Configuration
public class BaiduConfig{

    /**
     * 创建OkHttpClient实例
     *
     * @return OkHttpClient实例
     */
    @Bean
    public OkHttpClient httpClient(){
        return new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
    }

    /**
     * 百度API配置属性
     */
    @Component
    @ConfigurationProperties(prefix = "baidu.api")
    public static class BaiduApiProperties{
        /**
         * 应用ID
         */
        private String appId;

        /**
         * API密钥
         */
        private String apiKey;

        /**
         * 密钥
         */
        private String secretKey;

        /**
         * 获取token的URL
         */
        private String tokenUrl = "https://aip.baidubce.com/oauth/2.0/token";

        public String getAppId() {
            return appId;
        }

        public void setAppId(String appId) {
            this.appId = appId;
        }

        public String getApiKey() {
            return apiKey;
        }

        public void setApiKey(String apiKey) {
            this.apiKey = apiKey;
        }

        public String getSecretKey() {
            return secretKey;
        }

        public void setSecretKey(String secretKey) {
            this.secretKey = secretKey;
        }

        public String getTokenUrl() {
            return tokenUrl;
        }

        public void setTokenUrl(String tokenUrl) {
            this.tokenUrl = tokenUrl;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            BaiduApiProperties that = (BaiduApiProperties) o;
            return Objects.equals(appId, that.appId) &&
                   Objects.equals(apiKey, that.apiKey) &&
                   Objects.equals(secretKey, that.secretKey) &&
                   Objects.equals(tokenUrl, that.tokenUrl);
        }

        @Override
        public int hashCode() {
            return Objects.hash(appId, apiKey, secretKey, tokenUrl);
        }

        @Override
        public String toString() {
            return "BaiduApiProperties{" +
                   "appId='" + appId + '\'' +
                   ", apiKey='" + apiKey + '\'' +
                   ", secretKey='" + secretKey + '\'' +
                   ", tokenUrl='" + tokenUrl + '\'' +
                   '}';
        }
    }
}
